const jwt = require("jsonwebtoken");
const whiteList = [
    "/login",
    "/register",
    "/getcaptcha",
    "/getuser"
]
module.exports = ()=>{
    return async (ctx,next)=>{
        if(whiteList.includes(ctx.path)){
            await next();
            return;
        } 
        let token = ctx.request.headers.token;
        if(!token){
            ctx.body = {
                code:4,
                msg:"没有权限"
            }
            ctx.status = 401;
            return;
        }
        try {
            jwt.verify(token,"aj");
            await next();
        } catch (error) {
            ctx.body = {
                code:5,
                msg:"效验失败",
                error
            }
        }
    }

}